﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLCMS.Data.Model;
using DotNet.Common.NHibernateUtil;
using NHibernate;
using NHibernate.Criterion;
using DotNet.Common.Command;
using ZLCMS.Business.Shop.Commands.ShoppingCart;
using Newtonsoft.Json;
using ZLCMS.Web.SystemManage.Basics.ProductSKU;
using ZLCMS.Web.Wap.Code;
using ZLCMS.Business.Shop.Data;

namespace ZLCMS.Web.ShopCart
{
    public partial class ShoppingCartConfirm : WebMemberPageBase
    {
        protected MemberInfo _memberDb;
        protected decimal _totalPrice = 0; //合计
        protected decimal _totalWeight = 0;//商品重量
        protected int _totalCount = 0; //商品数量
         public   bool  existNormalPro = false;

        protected decimal _shippingPrice //运费
        {
            get
            {
                return 0;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            _memberDb = Master.LoadMember();

            if (Request["products"] == null)
            {
                Server.Transfer("/ShopCart/ShoppingCart.aspx");
            }
            //初始化全局变量值
            InitializeGlobalValue();

            //初始化控件值
            InitializeContent();
        }

        private void InitializeGlobalValue()
        {
            _memberDb = Master.LoadMember();
        }

        private void InitializeContent()
        {
            if (!IsPostBack)
            {
                using (ISession session = SessionFactoryHelper.CreateSession())
                {

                    IList<DataProvince> provinceList = session.CreateCriteria<DataProvince>().List<DataProvince>();

                    sel_province.DataSource = provinceList.ToList();

                    sel_province.DataValueField = "Code";
                    sel_province.DataTextField = "Name";
                    sel_province.DataBind();

                    BindProductData(session);
                    BindShippingData(session);
                    BindPaymentData(session);
                    BindAddressData(session);
                }
            }
        }

        private void BindProductData(ISession session)
        {
            CommandResult res = new LoadShoppingCartProductsCommand().Execute(new LoadShoppingCartProductsParameter() { Kind = Request["kind"] }) as CommandResult;

            IList<ShopShoppingCart> productList = (List<ShopShoppingCart>)res.Result;

            //未登录,从COOKIE中获取购物车产品
            List<ShopCartCookiesInfo> accountProducts = JsonConvert.DeserializeObject<List<ShopCartCookiesInfo>>(Request["products"]);

            foreach (ShopShoppingCart c in productList)
            {
                bool isMatch = false;
                foreach (ShopCartCookiesInfo p in accountProducts)
                {
                    if (p.ProductSkuCode == c.ProductSkuCode)
                    {
                        isMatch = true;
                        _totalCount += c.ProductCount;
                        _totalPrice += c.TotalPrice;
                    }
                }
                if (isMatch == false)
                {
                    productList.Remove(c);
                }
            }
            
            rptPro.DataSource = productList;
            rptPro.DataBind();

                //判断是否有虚拟产品
           
            foreach (ShopShoppingCart c in productList)
            {
                    ShopProductInfo product = session.CreateCriteria<ShopProductInfo>().Add(Expression.Eq("ProductNo", c.ProductNo)).UniqueResult<ShopProductInfo>();
                    if(product!=null)
                    {
                    if(product.Kind=="shop")
                    {
                            existNormalPro = true;
                    }
                    }
            }
        }

        private void BindAddressData(ISession session)
        {
            rp_addr.DataSource = session.CreateCriteria<MemberAddress>().Add(Expression.Eq("MemberAccount", _memberDb.AccountId)).AddOrder(Order.Desc("Status")).List<MemberAddress>();
            rp_addr.DataBind();
        }

        private void BindPaymentData(ISession session)
        {
            rp_pay.DataSource = session.CreateCriteria<ShopPaymentInfo>().Add(Expression.Eq("Kind", "WEB")).List<ShopPaymentInfo>();
            rp_pay.DataBind();
        }

        private void BindShippingData(ISession session)
        {
            rp_shipping.DataSource = session.CreateCriteria<ShopShippingInfo>().List<ShopShippingInfo>();
            rp_shipping.DataBind();
        }

        public string GetSkuItemHtml(string paramCode)
        {
            List<SKUGroup> grouplist = JsonConvert.DeserializeObject<List<SKUGroup>>(paramCode);
            string itemList = "";
            foreach (SKUGroup cd in grouplist)
            {
                itemList += "<span>[" + cd.ItemName + "]</span> ";
            }
            return itemList;
        }

    }
}
